<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Inter\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class TagsController extends InterController {
  function __construct() {
	   parent::__construct();
       $this->cat = D("Category");
	  
   }
    /* 文档模型频道页 */
	public function index(){
      echo "Hello 你在干什么";
	}

	/* 获取等等标签 r*/
	public function getTags(){
        $uid = intval($_POST['uid']);
		$status['code'] = 0;
		$arr = M("category")->field("id,title")->where(array('status'=>1))->order("sort ASC")->select(); 
		$arrz = M("category")->field("id")->where(array('status'=>0))->select();  
		if($uid<1)
		{
			
			$rs['tags'] = $arr;
            $status['data'] = $rs;
            $this->ajaxReturn($status);
		}
				if(!empty($arrz))
		{
			foreach($arrz as $k=>$v)
		    {
			   $noids[] = $v["id"];
            }
		}
				if(!empty($arr))
		{
			foreach($arr as $k=>$v)
		    {
			   $zong[] = $v["id"];
            }
		}
        $rs = M("member_tag")->where(array('uid'=>$uid))->find();
	    if(!empty($rs))
		{   $list = array();
		    $new = array();
			$list = json_decode($rs['tags'],true);
			$i=0;
			 foreach($list as $kk=>$v)
			{
				 //剔除禁dddd用的分类
			  if(in_array($v["id"],$noids))
			  {				
                
			  }else if(!in_array($v["id"],$zong)){
				   //剔除已经dd删除的分类
				 
			  } else{
				  
                $ids[] = $v["id"]; 
				$new[$i] = $v;
					$i++;
			  }
			}
			//array_fill($list);
           //获取为收dd藏dd的标签
	              foreach($arr as $ke=>$va)
				{
		         		  if(!in_array($va['id'],$ids))
					{
                       $no[] = $va;
					}
				}		   
            $rss['yes'] = $new;
			$rss['no'] = $no;
            $status['data'] = $rss;
            $this->ajaxReturn($status);
		}else
	    {
			$rs['uid'] = $uid;
			$rs['tags'] = $arr;
            $status['data'] = $rs;
            $this->ajaxReturn($status);
		}
	}

		/* 更新用户标签 */
	public function upTags(){
        $uid = intval($_POST['uid']);
		$tags = $_POST['tags'];
		$status['code'] = 1;
				if(!$tags)
		{
			
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		if($uid>0)
		{
		  $counts = M("member_tag")->where(array('uid'=>$uid))->count();
		  if($counts>0)
			{
			   M("member_tag")->where(array('uid'=>$uid))->save(array('tags'=>$tags));
			}else
			{
                M("member_tag")->add(array('tags'=>$tags,'uid'=>$uid));
			}
         
		  $rs['uid'] = $uid;
		  $rs['tags'] = json_decode($tags,true); //print_R($rs['tags']);
		    $status['code'] = 0;
            $status['data'] = $rs;
            $this->ajaxReturn($status);
		}else
		{
            $status['msg'] = '更新失败';
            $this->ajaxReturn($status);
		}
	}
}
